package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.view.WaitingView;

public class WebLoadingActivity extends AppCompatActivity {
    private WaitingView wv;
    private ProgressBar progressBar;
    private Button btn;
    private WebView webView;
    private static final int LOADFINISH=100;
    private String url="http://60.205.211.116/Api/Vote/voteList?studentId=505&SessionID=db12847803c06413d91d8639de92d73a";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pupop);
        wv=(WaitingView)findViewById(R.id.id_wv);
        progressBar=(ProgressBar)findViewById(R.id.progress);
        webView=(WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new JsHook(), "Link");
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageFinished(WebView view, String url) {
                wv.setVisibility(View.GONE);
            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                if(newProgress==LOADFINISH){
                    progressBar.setVisibility(View.GONE);
                }else {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });

    }
    @Override
    protected void onResume() {
        super.onResume();
        wv.setVisibility(View.VISIBLE);
        webView.loadUrl(url);
    }
    public class JsHook{
        @JavascriptInterface
        public void toVote(String url){
            Intent intent =new Intent(WebLoadingActivity.this,MatchDetailActivity.class);
            intent.putExtra("url",url);
            startActivity(intent);
        }
    }
}
